﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Ratatoskr.Toolbox
{
    public class SpriteDrawRepeater : SpriteDrawRepeater<Component>
    {
        public SpriteDrawRepeater(string type, string repeatType, bool open = false, float priority = 0, bool enabled = true, bool single = false)
            : base(type, repeatType, open, priority, enabled, single)
        {
        }

        public SpriteDrawRepeater(string type, bool open = false, float priority = 0, bool enabled = true, bool single = false)
            : base(type, open, priority, enabled, single)
        {
        }

        public SpriteDrawRepeater(bool open = false, float priority = 0, bool enabled = true, bool single = false)
            : base(open, priority, enabled, single)
        {
        }

    }

    public class SpriteDrawRepeater<T> : RepeaterComponent<T> where T : class
    {
        public SpriteDrawRepeater(string type, string repeatType, bool open = false, float priority = 0, bool enabled = true, bool single = false)
            : base(type, repeatType, open, priority, enabled, single)
        {
        }

        public SpriteDrawRepeater(string type, bool open = false, float priority = 0, bool enabled = true, bool single = false)
            : this(type, type, open, priority, enabled, single)
        {
        }

        public SpriteDrawRepeater(bool open = false, float priority = 0, bool enabled = true, bool single = false)
            : this("SpriteDrawServer", open, priority, enabled, single)
        {
        }

        protected override void Repeat(object sender, ServerComponentEventArgs e)
        {
            if (!_enabled)
                return;
            SpriteTransform t;
            if (Parent.TryGetComponent<SpriteTransform>("SpriteTransform", out t))
            {
                Matrix m = (Matrix)e.Args[2] * t.Matrix;
                Color c = (Color)e.Args[3];
                c = new Color(
                    c.R * t.Color.R / 65025,  // 255 * 255
                    c.G * t.Color.G / 65025,
                    c.B * t.Color.B / 65025,
                    c.A * t.Color.A / 65025);
                DoTrigger(new object[] { e.Args[0], e.Args[1], m, c });
            }
            else DoTrigger(e.Args);
        }
    }
}